package com.lsl.gulimall.product.service.impl;

import com.lsl.gulimall.product.entity.BrandEntity;
import com.lsl.gulimall.product.entity.CategoryEntity;
import com.lsl.gulimall.product.service.BrandService;
import com.lsl.gulimall.product.service.CategoryService;
import com.lsl.gulimall.product.vo.response.BrandOutVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsl.common.utils.PageUtils;
import com.lsl.common.utils.Query;

import com.lsl.gulimall.product.dao.CategoryBrandRelationDao;
import com.lsl.gulimall.product.entity.CategoryBrandRelationEntity;
import com.lsl.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    BrandService brandService;
    @Autowired
    CategoryService categoryService;

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void saveCategoryBrandRelationEntity(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandService.getById(brandId);

        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(queryWrapper);

        for (CategoryBrandRelationEntity categoryBrandRelationEntity : categoryBrandRelationEntities) {
            if (catelogId.equals(categoryBrandRelationEntity.getCatelogId())) {
                catelogId = null;
                return;
            }
        }
        if (catelogId != null) {
            CategoryEntity categoryEntity = categoryService.getById(catelogId);
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
            categoryBrandRelation.setBrandName(brandEntity.getName());
            this.save(categoryBrandRelation);
        }
    }

    @Override
    public List<BrandOutVo> getBrandsRelationByCatId(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("catelog_id",catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationService.list(queryWrapper);

        List<Long> BrandIds = categoryBrandRelationEntities.stream().map(
                (item) ->
                        item.getBrandId()
        ).collect(Collectors.toList());

        List<BrandEntity> brandEntities = (List<BrandEntity>) brandService.listByIds(BrandIds);
        ArrayList<BrandOutVo> brandOutVos = new ArrayList<>();
        for (BrandEntity brandEntity:brandEntities) {
            BrandOutVo brandOutVo = new BrandOutVo();
            brandOutVo.setBrandId(brandEntity.getBrandId());
            brandOutVo.setBrandName(brandEntity.getName());
            brandOutVos.add(brandOutVo);
        }
        return brandOutVos;
    }
}